package cn.com.nxt.txb.httpdelegate;

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.com.nxt.txb.HttpProtocol;
import cn.com.nxt.txb.ResponseStatus;
import cn.com.nxt.txb.SampleApplicationLike;
import cn.com.nxt.txb.model.BaseModel;
import cn.com.nxt.txb.util.AppConstant;

/**
 * Created by zhangling on 2017/7/22.
 * 链接相关接口/
 */

public class LinkDelegate {
    private HttpProtocol protocol;

    public LinkDelegate(HttpProtocol protocol) {
        this.protocol = protocol;
    }
    //获取相关链接地址
    public void linkerAddrs(final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.LINKER_ADDRS);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--linkerAddrs_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else {
                    if (code.equals("10008"))
                        SampleApplicationLike.isNotBusniessUser = true;
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
                }
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--linkerAddrs_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    //3. 短信邀请(用于会议邀请等)
    //CodeSmsOverLimit = 10013  短信发送频率过高，请稍后再试,
    //CodeSmsCodeInvalid =10014   短信验证码无效,
    //CodeSmsMsgSendError = 10015 短信发送失败
    public void smsInviter(String param,final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.SMS_INVITER);
        rp.setBodyContent(param);
        x.http().post(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--smsInviter_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else if (code.equals("10013")){
                    protocol.failure("短信发送频率过高，请稍后再试", flag, ResponseStatus.REQUEST_FAILURE);
                }else if (code.equals("10014")){
                    protocol.failure("短信验证码无效", flag, ResponseStatus.REQUEST_FAILURE);
                }else if (code.equals("10015")){
                    protocol.failure("短信发送失败", flag, ResponseStatus.REQUEST_FAILURE);
                }else {
                    if (code.equals("10008"))
                        SampleApplicationLike.isNotBusniessUser = true;
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
                }
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--smsInviter_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }
    //获取验证码(用于注册以及重置密码)
    //CodeSmsOverLimit = 10013  短信发送频率过高，请稍后再试,
    //CodeSmsCodeInvalid =10014   短信验证码无效,
    //CodeSmsMsgSendError = 10015 短信发送失败
    public void smsVertify(String param,final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.SMS_VERTIFY);
        rp.setBodyContent(param);
        x.http().post(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--smsVertify_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else if (code.equals("10013")){
                    protocol.failure("短信发送频率过高，请稍后再试", flag, ResponseStatus.REQUEST_FAILURE);
                }else if (code.equals("10014")){
                    protocol.failure("短信验证码无效", flag, ResponseStatus.REQUEST_FAILURE);
                }else if (code.equals("10015")){
                    protocol.failure("短信发送失败", flag, ResponseStatus.REQUEST_FAILURE);
                }else {
                    if (code.equals("10008"))
                        SampleApplicationLike.isNotBusniessUser = true;
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
                }
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--smsVertify_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    //2.验证码校验
    //CodeSmsOverLimit = 10013  短信发送频率过高，请稍后再试,
    //CodeSmsCodeInvalid =10014   短信验证码无效,
    //CodeSmsMsgSendError = 10015 短信发送失败
    public void smsVertifyCheck(String code,String phone,final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.SMS_VERTIFY_CHECK);
        rp.setHeader("code", code);
        rp.setHeader("phone", phone);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--smsVertifyCheck_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else if (code.equals("10013")){
                    protocol.failure("短信发送频率过高，请稍后再试", flag, ResponseStatus.REQUEST_FAILURE);
                }else if (code.equals("10014")){
                    protocol.failure("短信验证码无效", flag, ResponseStatus.REQUEST_FAILURE);
                }else if (code.equals("10015")){
                    protocol.failure("短信发送失败", flag, ResponseStatus.REQUEST_FAILURE);
                }else {
                    if (code.equals("10008"))
                        SampleApplicationLike.isNotBusniessUser = true;
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
                }
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--smsVertifyCheck_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

}
